package me.minecraft.plugin.filemonitor;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @author langle__
 * @version 1.0
 */
public class ObserverThread extends BukkitRunnable {

    private final Set<FileListener> listeners = Collections.synchronizedSet(new HashSet<>());

    private final JavaPlugin plugin;

    public ObserverThread(JavaPlugin plugin) {
        this.plugin = plugin;
    }

    @Override
    public void run() {
        synchronized (listeners) {
            listeners.forEach(FileListener::canExecuteCommand);
        }
    }

    public void start() {
        this.runTaskTimer(plugin, 20, 20);
    }

    public void stop() {
        this.cancel();
    }

    public void addFileListener(FileListener listener) {
        listeners.add(listener);
    }

    public void delFileListener(FileListener listener) {
        listeners.remove(listener);
    }

    public void clearListeners() {
        listeners.clear();
    }

}
